var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
  data:{
    orderList: [],
    page: 1,
    size: 10,
    orderStatus:null,
    loadmoreText: '正在加载更多数据',
    nomoreText: '全部加载完成',
    nomore: false,
    totalPages: 1,
    currentIndex:'-1',
    tabsList:[
      {id:"-1",text:'全部',},
      {id:"0",text:'待付款'},
      {id:"201",text:'待发货'},
      {id:"300",text:'待收货'},
      {id:"301",text:'已完成'},
    ]
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    // 页面显示
    this.setData({
      currentIndex:options._s,
      orderStatus:options._s
    }) 
    wx.showLoading({
      title: '加载中...',
      success: function () {
      }
    });
    this.getOrderList();
   
  },

  /**
       * 页面上拉触底事件的处理函数
       */
  onReachBottom: function () {
    this.getOrderList()
  },
  refreshList:function(id){ 
    this.setData({
      currentIndex:id,
      orderStatus:id,
      orderList: [],
      page: 1,
      totalPages:1
    }) 
    this.getOrderList(); 
  },
  changeTab:function(e){
    this.setData({
      currentIndex: e.currentTarget.dataset['index'],
      orderStatus: e.currentTarget.dataset['index'],
      orderList: [],
      page: 1,
      totalPages:1,
    }) 
    this.getOrderList(); 
  },
  getOrderList(){ 
    let that = this;
    if (that.data.totalPages <= that.data.page - 1) {
      that.setData({
        nomore: true
      })
      return;
    }
    let params={
      page: that.data.page, 
      size: that.data.size
    }
    if(that.data.orderStatus>-1){
        params.orderStatus = that.data.orderStatus
      }
    util.request(api.OrderList, params).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          orderList: that.data.orderList.concat(res.data.data),
          page: res.data.currentPage + 1,
          totalPages: res.data.totalPages
        });
        wx.hideLoading();
      }
    });
  },
  payOrder(event){
      let that = this;
      let orderIndex = event.currentTarget.dataset.orderIndex;
      let order = that.data.orderList[orderIndex];
      wx.redirectTo({
          url: '/pages/pay/pay?orderId=' + order.id + '&actualPrice=' + order.actualPrice,
      })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){ 
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})
